#!/usr/bin/perl



$ifconfig_out = readpipe("ifconfig eth1");

#print $ifconfig_out;

@fields = split(" ", $ifconfig_out ); 

#print @fields;

$my_mac = $fields[4];

 
 
#print "mac=$my_mac\n";


open(mac_mapping_file, "<mac.addr"); 
while(<mac_mapping_file>) 
{ 
    my($line) = $_; 
    chomp($line); 
    
    if ( $line =~ /^\s*$/ || $line =~ /\#/ ) 
    { 
	next; 
    }

    @entries = split(' ', $line); 
    my($ip) = $entries[0]; 
    my($mac) = $entries[1]; 
    $ip_mac_map{ $ip } = $mac;
    if ($mac eq $my_mac) { 
	$my_ip = $ip; 
	break; 
    }
}

close(mac_mapping_file);


print "-----------IP MAC address mapping------------\n";
for my $ip (keys( %ip_mac_map ) ) { 
   print "$ip = $ip_mac_map{$ip}\n"; 
}
print "--------------------------------------------\n\n";

if ($my_ip) { 
    print "Local MAC = $my_mac\n"; 
    print "Local IP  = $my_ip\n"; 
    open(localhost_file, ">/etc/localhost"); 
	print localhost_file "$my_ip\n" ;    
	close(localhost_file); 
} else { 
    print "Cannot find ip address matching the local mac address $my_mac, check your mac.addr file\n"; 
    exit(1); 
}


#@TODO: call the network_setup script

print "setting local ip address to $my_ip\n"; 

system("./network_setup", $my_ip); 






